package com.mla.fastdfs.controller;

import com.github.pagehelper.PageInfo;
import com.mla.fastdfs.commom.Result;
import com.mla.fastdfs.pojo.Group;
import com.mla.fastdfs.service.GroupService;
import com.mla.fastdfs.utils.Assert;
import com.mla.fastdfs.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Group record) {
        Assert.notNull(record.getGroupCore(), "分组编号不能为空");
        Assert.notNull(record.getGroupName(), "类型名称不能为空");
        if (record.getId() != null)
            groupService.updateById(record);
        else {
            record.setCreateTime(DateUtil.getStrTime());
            groupService.save(record);
        }
        return Result.ok();
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(Integer id) {
        Assert.notNull(id, "id不能为空");
        groupService.removeById(id);
        return Result.ok();
    }

    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
    public Result selectByPrimaryKey(Integer id) {
        Assert.notNull(id, "id不能为空");
        Group info = groupService.getById(id);
        return Result.ok(info);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result getList(Group record) {
        /**条件判断**/

        List<Group> list = groupService.getList(record);
        return Result.ok(list);
    }

    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    public Result pageList(Group record,
                           @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        /**条件判断**/

        PageInfo<Group> info = groupService.pageList(record, pageNo, pageSize);
        return Result.ok(info);
    }
}
